package com.adam.example.newssearches8_2_0.common;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.util.Assert;

import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PagedData<T>{

    /**
     * 1-based
     */
    private int pageNo;
    private int pageSize;
    private int totalPages;
    private long totalCount;
    private List<T> data;

    public static <V> PagedData<V> of(int pageNo, int pageSize, long totalCount, List<V> data) {
        Assert.isTrue(pageNo > 0, "invalid pageNo:" + pageNo);
        Assert.isTrue(pageSize > 0, "invalid pageSize:" + pageSize);
        Assert.isTrue(totalCount >= 0, "invalid totalCount:" + totalCount);
        int totalPages = (int) Math.ceil(1.0 * totalCount / pageSize);
        return new PagedData<>(pageNo, pageSize, totalPages, totalCount, data);
    }

}
